package tw.com.softleader.fire.setting.web;

import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import tw.com.softleader.fire.setting.entity.AddrZipEntity;
import tw.com.softleader.fire.setting.service.AddrZipService;

import com.opensymphony.xwork2.ActionSupport;

public class AddrZipAction extends ActionSupport {

	private Logger log = LoggerFactory.getLogger(AddrZipAction.class);

	@Autowired
	private AddrZipService service;
	
	private List<AddrZipEntity> models;
	private AddrZipEntity model;
	
	public List<AddrZipEntity> getModels() {
		return models;
	}
	
	public void setModels(List<AddrZipEntity> models) {
		this.models = models;
	}
	
	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}
	
	public AddrZipEntity getModel() {
		return model;
	}

	public void setModel(AddrZipEntity model) {
		this.model = model;
	}

	@SkipValidation
	public String execute() throws Exception {
		log.debug("execute...");
		try {
			models = service.getAll();
			
			log.debug("Models = {}",models);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String select(){
		
		System.out.println("I am in select");
		
		log.debug("select...");
		try {
/*			models = service.search(this.getModel());*/
			models = service.search(model.getAddrCode());

			log.debug("Models = {}",models);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String delete() {
		log.debug("do delete");
		log.debug("Model = {}", model);
		try {
			service.delete(model);
		} catch (Exception e) {
			log.debug("本資料已被移除");
		}
		
		models = service.getAll();
		log.debug("Models = {}", models);
		
		return SUCCESS;
	}
	
	public String update() {
		log.debug("do update");
		model.setModifier("guest");
		model.setModifiedTime(LocalDateTime.now());
		log.debug("Model = {}", model);
		try {
			service.update(model);
		} catch (Exception e) {
			log.debug("資料項目不存在");
		}
		
		models = service.getAll();
		log.debug("Models = {}", models);
		
		return SUCCESS;
	}
	
	public String insert() {
		log.debug("do insert");
		
		model.setId(null);
		model.setCreator("guest");
		model.setModifier("guest");
		model.setCreateTime(LocalDateTime.now());
		model.setModifiedTime(LocalDateTime.now());
		
		log.debug("Model = {}", model);
		try {
			service.insert(model);
		} catch (Exception e) {
			log.debug("新增項目失敗");
			e.printStackTrace();
		}
		
		models = service.getAll();
		log.debug("Models = {}", models);
		
		return SUCCESS;
	}

	
}
